/*
 * 描述： <描述>
 * 修改人： rain
 * 修改时间： 2016年9月30日
 * 项目： rainhy-auth
 */
package com.rainhy.auth.authItemManager.springdata;

import java.util.Collection;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;

import com.rainhy.auth.authItemManager.springdata.entity.AuthItemRefEntity;
import com.rainhy.auth.authItemManager.springdata.service.AuthItemRefEntityService;
import com.rainhy.auth.context.AuthItemRef;
import com.rainhy.auth.context.AuthManager;
import com.rainhy.auth.context.AuthSessionContext;
import com.rainhy.core.util.AssertUtils;

/**
 * SpringData 权限管理器
 * 
 * @author rain
 * @version [版本号, 2016年9月30日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class SpringDataAuthManager implements AuthManager {
    
    @Resource(name = "com.rainhy.auth.authItemManager.springdata.service")
    private AuthItemRefEntityService service;
    
    @Override
    @Transactional
    public void persist(AuthItemRef authItemRef) {
        AssertUtils.notNull(authItemRef, "权限引用不能为 null.");
        AssertUtils.notNull(authItemRef.getAuthItem(), "权限引用中的权限不能为 null.");
        AssertUtils.notBlank(authItemRef.getAuthItem().getKey(), "权限引用中的权限 key 不能为 null.");
        
        if (authItemRef instanceof AuthItemRefEntity) {
            persist((AuthItemRefEntity) authItemRef);
        }
        
        DateTime now = DateTime.now();
        
        /// 查询条件
        AuthItemRefEntity authItemRefEntity = new AuthItemRefEntity();
        authItemRefEntity.setAuthItemKey(authItemRef.getAuthItem().getKey());
        authItemRefEntity.setAuthRefType(authItemRef.getAuthRefType());
        authItemRefEntity.setAuthRefId(authItemRef.getAuthRefId());
        
        authItemRefEntity = service.findByAuthItemRefEntity(authItemRefEntity);
        if (authItemRefEntity == null) {
            authItemRefEntity = new AuthItemRefEntity();
            authItemRefEntity.setCreatedDate(now);
            authItemRefEntity.setCreatedBy(AuthSessionContext.getOperatorIdFromSession());
        }
        authItemRefEntity.setAuthItem(authItemRef.getAuthItem());
        authItemRefEntity.setAuthItemKey(authItemRef.getAuthItem().getKey());
        authItemRefEntity.setAuthRefId(authItemRef.getAuthRefId());
        authItemRefEntity.setAuthRefType(authItemRef.getAuthRefType());
        authItemRefEntity.setEffectiveDate(authItemRef.getEffectiveDate());
        authItemRefEntity.setInvalidDate(authItemRef.getInvalidDate());
        authItemRefEntity.setLastModifiedBy(AuthSessionContext.getOperatorIdFromSession());
        authItemRefEntity.setLastModifiedDate(now);
        
        service.save(authItemRefEntity);
    }
    
    @Override
    public Collection<AuthItemRef> queryAuthItemRef(MultiValueMap<String, String> refType2RefIdMapping) {
        return service.queryAuthItemRef(refType2RefIdMapping);
    }
}
